package com.auto.survey.activity.monitoring.station

import android.graphics.Color
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import androidx.lifecycle.lifecycleScope
import com.android.identity.util.UUID
import com.auto.survey.R
import com.auto.survey.activity.monitoring.station.CreateStationActivity.Companion.CREATE_STATION_GUESTER_RESULT_OK
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_OBJECT_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_PROJET_ID
import com.auto.survey.activity.monitoring.dao.MonitoringRepository
import com.auto.survey.activity.monitoring.entity.MonitoringStation
import com.auto.survey.base.BaseActivity
import com.auto.survey.util.Helper
import com.auto.survey.widget.CentreToast
import com.auto.survey.widget.CommonTitle.Companion.TEXT_POSITON_RIGHT
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 手动创建测站
 */
class CreateStationGuesterActivity : BaseActivity(){

    private var projectId:Int = -1
    private var objectId:Int = -1

    private val database = MonitoringRepository()

    override fun initView() {

        projectId = intent.getIntExtra(KEY_INTENT_PROJET_ID, -1)
        objectId = intent.getIntExtra(KEY_INTENT_OBJECT_ID, -1)

        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.action_bar)

        val station_name = findViewById<EditText>(R.id.edit_create_montoring_station_name)
        val station_uuid = findViewById<EditText>(R.id.edit_create_montoring_station_uuid)
        val station_high = findViewById<EditText>(R.id.edit_create_montoring_station_hight)
        val station_x = findViewById<EditText>(R.id.edit_create_montoring_station_X)
        val station_y = findViewById<EditText>(R.id.edit_create_montoring_station_Y)
        val station_z = findViewById<EditText>(R.id.edit_create_montoring_station_z)

        station_uuid.setText(UUID.randomUUID().toString())

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.action_bar)
        mComment_title?.setTitle("新建测站")

        mComment_title?.setRightTitle("保存")
        mComment_title?.setTitleColor(Color.WHITE, TEXT_POSITON_RIGHT)
        mComment_title?.setOnRightTextClickListener(Helper.debounce{
            val name = station_name.text.toString()
            if (TextUtils.isEmpty(name)) {
                CentreToast.instance.show("名称不可为空", false)
                return@debounce
            }

            if (objectId == -1) {
                CentreToast.instance.show("监测对象未指定", false)
                return@debounce
            }

            var uid = station_uuid.text.toString()
            var h = station_high.text.toString()
            var x = station_x.text.toString()
            var y = station_y.text.toString()
            var z = station_z.text.toString()

            if (TextUtils.isEmpty(h)) {
                CentreToast.instance.show("仪器高不可为空", false)
                return@debounce
            }

            if (TextUtils.isEmpty(x)) {
                CentreToast.instance.show("X不可为空", false)
                return@debounce
            }

            if (TextUtils.isEmpty(y)) {
                CentreToast.instance.show("Y不可为空", false)
                return@debounce
            }

            if (TextUtils.isEmpty(z)) {
                CentreToast.instance.show("Z不可为空", false)
                return@debounce
            }

            val creatTime = System.currentTimeMillis()


            if (TextUtils.isEmpty(uid)) {
                uid = UUID.randomUUID().toString()
            }

            val station = MonitoringStation(monitoringObjectId= objectId, stationName = name,
                stationCode=uid, stationX = x.toDouble(), stationY = y.toDouble(), stationZ = z.toDouble(),
                stationHeight= h.toDouble(), stationDescription="", isReferenceStation= false,
                createTime = Helper.formatTimestamp(creatTime), updateTime ="")

            lifecycleScope.launch {
                val succ = database.createStation(station)
                withContext(Dispatchers.Main) {
                    if (succ.toInt() != -1) {
                        CentreToast.instance.show("添加成功", true)
                        setResult(CREATE_STATION_GUESTER_RESULT_OK)
                    } else {
                        CentreToast.instance.show("添加失败", true)
                    }
                    finish()
                }
            }

        })
    }

    override fun getLayoutId(): Int {

        return R.layout.activity_create_guester_monitoring_station
    }
}